package com.fyp.ptma.dao.service;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fyp.ptma.dao.repository.irepository.IUserDao;
import com.fyp.ptma.model.User;
import com.fyp.ptma.security.AuthorisedUser;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
	@Autowired
	private IUserDao userDAO;

	@Transactional
	public UserDetails loadUserByUsername(String email)
			throws UsernameNotFoundException {

		User user = userDAO.getUserByEmail(email.toLowerCase());
		if (user == null) {
			user = new User();
		}

		return new AuthorisedUser(user, new ArrayList<GrantedAuthority>());
	}

}
